#!/usr/bin/python
#Drew Carpenter & Jeff Gullett
#CS 4444
#Excavare 0.0
#March 29,2011
import os
import sys
import gtk
import pygtk
import makefilter
from open_workspace import *
from create_tabs import *
def save_file(self,widget):
		current=self.notebook.get_current_page()
		tm=self.treeview.get_model()
		iter=tm.get_iter(current)
		saved_once=tm.get_value(iter,5)
		file1=tm.get_value(iter,2)
		if file1 is not None:
			extType=file1.split(".")
			if extType[-1] != "txt":
				warning = gtk.MessageDialog(parent=self.excavare, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="You are trying to save a file of type "+extType[-1]+".\nExcavare cannot save this file type. If you would like to save what you have as a plain text file (.txt) press OK.\nThe original file has not been changed.")
				warn = warning.run()
				if warn ==gtk.RESPONSE_CANCEL:
					warning.destroy()
					return
				else:
					warning.destroy()
		if saved_once is False:
			filesel = gtk.FileChooserDialog(title='Save',parent=self.excavare,action=gtk.FILE_CHOOSER_ACTION_SAVE,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
			filesel.set_default_response(gtk.RESPONSE_OK)
			file_action=filesel.run()
			if file_action==gtk.RESPONSE_OK:
				scrolly=self.notebook.get_focus_child()
				view=scrolly.get_child()
				texty=view.get_child()
				buffy=texty.get_buffer()
				start=buffy.get_start_iter()
				end=buffy.get_end_iter()
				all_text=buffy.get_text(start,end)
				name=filesel.get_filename()
				extension=name.split(".")
				if len(extension)==1:
					temp_name=name+".txt"
				if extension[-1]!=".txt":
					temp_name=extension[0]+".txt"
				else:
					temp_name=name
				finalNameList=temp_name.split("/")
				if len(finalNameList)==1:
					finalNameList=temp_name.split("\\")
				finalName=finalNameList[-1]
				if os.path.exists(finalName):
					warning = gtk.MessageDialog(parent=filesel, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="The file already exists. Are you sure you want to repace it?")
					warn = warning.run()
					if warn ==gtk.RESPONSE_CANCEL:
						warning.destroy()
						return
					else:
						#erase the file
						erase = open(finalName,"w").close()
						warning.destroy()
				filesel.destroy()
				new_file = open(finalName,"w")
				new_file.write(all_text)
				new_file.close()
				current=self.notebook.get_current_page()
				tm=self.treeview.get_model()
				iter=tm.get_iter(current)
				tm.set_value(iter,0,finalName)
				tm.set_value(iter,2,finalName)
				tm.set_value(iter,5,True)
				new_label=gtk.Label(finalName)
				self.notebook.set_tab_label(scrolly,new_label)
			if file_action == gtk.RESPONSE_CANCEL:
				#if cancel is clicked, destroy file chooser dialog
				filesel.destroy()
		else:
			scrolly=self.notebook.get_focus_child()
			view=scrolly.get_child()
			texty=view.get_child()
			buffy=texty.get_buffer()
			start=buffy.get_start_iter()
			end=buffy.get_end_iter()
			all_text=buffy.get_text(start,end)
			finalName=tm.get_value(iter,2)
			new_file = open(finalName,"w")
			new_file.write(all_text)
			new_file.close()
def save_as_file(self,widget):
		current=self.notebook.get_current_page()
		tm=self.treeview.get_model()
		iter=tm.get_iter(current)
		saved_once=tm.get_value(iter,5)
		file1=tm.get_value(iter,2)
		if file1 is not None:
			extType=file1.split(".")
			if extType[-1] != "txt":
				warning = gtk.MessageDialog(parent=self.excavare, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="You are trying to save a file of type "+extType[-1]+".\nExcavare cannot save this file type. If you would like to save what you have as a plain text file (.txt) press OK.\nThe original file has not been changed.")
				warn = warning.run()
				if warn ==gtk.RESPONSE_CANCEL:
					warning.destroy()
					return
				else:
					warning.destroy()
		filesel = gtk.FileChooserDialog(title='Save As',parent=self.excavare,action=gtk.FILE_CHOOSER_ACTION_SAVE,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
		filesel.set_default_response(gtk.RESPONSE_OK)
		file_action=filesel.run()
		if file_action==gtk.RESPONSE_OK:
			scrolly=self.notebook.get_focus_child()
			view=scrolly.get_child()
			texty=view.get_child()
			buffy=texty.get_buffer()
			start=buffy.get_start_iter()
			end=buffy.get_end_iter()
			all_text=buffy.get_text(start,end)
			name=filesel.get_filename()
			extension=name.split(".")
			if len(extension)==1:
				temp_name=name+".txt"
			if extension[-1]!=".txt":
				temp_name=extension[0]+".txt"
			else:
				temp_name=name
			finalNameList=temp_name.split("/")
			if len(finalNameList)==1:
				finalNameList=temp_name.split("\\")
			finalName=finalNameList[-1]
			if os.path.exists(finalName):
				warning = gtk.MessageDialog(parent=filesel, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="The file already exists. Are you sure you want to repace it?")
				warn = warning.run()
				if warn ==gtk.RESPONSE_CANCEL:
					warning.destroy()
					return
				else:
						#erase the file
					erase = open(finalName,"w").close()
					warning.destroy()
			filesel.destroy()
			new_file = open(finalName,"w")
			new_file.write(all_text)
			new_file.close()
			current=self.notebook.get_current_page()
			tm=self.treeview.get_model()
			iter=tm.get_iter(current)
			tm.set_value(iter,0,finalName)
			tm.set_value(iter,2,finalName)
			tm.set_value(iter,5,True)
			new_label=gtk.Label(finalName)
			self.notebook.set_tab_label(scrolly,new_label)
		if file_action == gtk.RESPONSE_CANCEL:
				#if cancel is clicked, destroy file chooser dialog
			filesel.destroy()

def open_file(self,widget):
		#create the filechooser dialog box
		filesel = gtk.FileChooserDialog(title='Open',parent=self.excavare,action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
		filesel.set_default_response(gtk.RESPONSE_OK)
		#create filters for the filechooser dialog
		makefilter.filter_set(filesel)
		file_action = filesel.run()
		if file_action == gtk.RESPONSE_CANCEL:
			#if cancel is clicked, destroy file chooser dialog
			filesel.destroy()
		elif file_action == gtk.RESPONSE_OK:
			#if a file is chosen get the filename
			#this is an absolute filename
			filename = filesel.get_filename()
			create_new_tab(self,filename)
			filesel.destroy()			
			
def save_workspace(self,widget):
		if self.is_workspace is False:
			filesel = gtk.FileChooserDialog(title='Save Workspace',parent=self.excavare,action=gtk.FILE_CHOOSER_ACTION_SAVE,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
			filesel.set_default_response(gtk.RESPONSE_OK)
			file_action=filesel.run()
			if file_action==gtk.RESPONSE_OK:
				name=filesel.get_filename()
				extension=name.split(".")
				if len(extension)==1:
					temp_name=name+".wksp"
				if extension[-1]!="wksp":
					temp_name=extension[0]+".wksp"
				else:
					temp_name=name
				finalNameList=temp_name.split("/")
				if len(finalNameList)==1:
					finalNameList=temp_name.split("\\")
				finalName=finalNameList[-1]
				if os.path.exists(finalName):
					warning = gtk.MessageDialog(parent=filesel, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="The file already exists. Are you sure you want to repace it?")
					warn = warning.run()
					if warn ==gtk.RESPONSE_CANCEL:
						warning.destroy()
						return
					else:
						#erase the file
						erase = open(finalName,"w").close()
						warning.destroy()
				filesel.destroy()
				workspace = open(finalName,"w")
				self.excavare.set_title(finalName)
				workspace.write("title\t"+temp_name)
				workspace.write("\ntab_numbers\t"+str(self.tab_nums))
				model=self.treeview.get_model()
				iter=model.get_iter_first()
				file_entry=model.get_value(iter,2)
				workspace.write("\n"+file_entry)
				#workspace.write("\nTextEditor\t"+str(model.get_value(iter,2)))
				if iter is not None:
					for i in range(0,self.tab_nums):
						iter=model.iter_next(iter)
						if iter is not None:
							workspace.write("\n")
							file_entry=model.get_value(iter,2)
							workspace.write(file_entry)
						else:
							break
				workspace.close()
				self.is_workspace=True
			else:
				filesel.destroy()
		else:
			finalName=self.excavare.get_title()
			file = open(finalName,"r")
			first_line=file.readline()
			line=first_line.split("\t")
			temp_name=line[-1]
			file.close()
			erase = open(finalName,"w").close()
			workspace = open(finalName,"w")
			workspace.write("title\t"+temp_name)
			workspace.write("tab_numbers\t"+str(self.tab_nums))
			model=self.treeview.get_model()
			iter=model.get_iter_first()
			workspace.write("\nTextEditor\t"+str(model.get_value(iter,2)))
			if iter is not None:
				for i in range(0,self.tab_nums):
					iter=model.iter_next(iter)
					if iter is not None:
						workspace.write("\n")
						file_entry=model.get_value(iter,2)
						workspace.write(file_entry)
					else:
						break
			workspace.close()
			
def new_file(self,widget):
		newScrolledWindow,newTextView=create_scrolled_window()
		newTab=gtk.Label("new")
		self.notebook.append_page(newScrolledWindow,newTab)
		self.tab_nums=self.tab_nums+1
		#put new textview in list of textviews
		self.textviews.append(newTextView)
		self.list_store.append(["new",self.tab_nums,None,1,0,False])
		self.notebook.show_all()